---
layout: "default"
title: "withUnsafePointer"
description: "Swift documentation for 'withUnsafePointer': Invokes the given closure with a pointer to the given argument."
keywords: "withUnsafePointer,func,swift,documentation"
root: "/v4.2"
---

<div class="declaration" id="func-withunsafepointer-t-result-to_-t_-unsafepointer-t-throws-result">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-withunsafepointer-t-result-to_-t_-unsafepointer-t-throws-result">func withUnsafePointer&lt;T, Result&gt;(<wbr>to:<wbr> T, _: (UnsafePointer&lt;T&gt;) throws -&gt; Result)</a>
        
<div class="comment collapse" id="comment-func-withunsafepointer-t-result-to_-t_-unsafepointer-t-throws-result"><div class="p">
    <p>Invokes the given closure with a pointer to the given argument.</p>

<p>The <code>withUnsafePointer(to:_:)</code> function is useful for calling Objective-C
APIs that take in parameters by const pointer.</p>

<p>The pointer argument to <code>body</code> is valid only during the execution of
<code>withUnsafePointer(to:_:)</code>. Do not store or return the pointer for later
use.</p>

<p><strong>Parameters:</strong>
  <strong>value:</strong> An instance to temporarily use via pointer.
  <strong>body:</strong> A closure that takes a pointer to <code>value</code> as its sole argument. If
    the closure has a return value, that value is also used as the return
    value of the <code>withUnsafePointer(to:_:)</code> function. The pointer argument
    is valid only for the duration of the function&#39;s execution.
    It is undefined behavior to try to mutate through the pointer argument
    by converting it to <code>UnsafeMutablePointer</code> or any other mutable pointer
    type. If you need to mutate the argument through the pointer, use
    <code>withUnsafeMutablePointer(to:_:)</code> instead.
<strong>Returns:</strong> The return value, if any, of the <code>body</code> closure.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func withUnsafePointer&lt;T, Result&gt;(to value: T, _ body: (UnsafePointer&lt;T&gt;) throws -&gt; Result) rethrows -&gt; Result</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-withunsafepointer-t-result-to_-inout-t_-unsafepointer-t-throws-result">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-withunsafepointer-t-result-to_-inout-t_-unsafepointer-t-throws-result">func withUnsafePointer&lt;T, Result&gt;(<wbr>to:<wbr> inout T, _: (UnsafePointer&lt;T&gt;) throws -&gt; Result)</a>
        
<div class="comment collapse" id="comment-func-withunsafepointer-t-result-to_-inout-t_-unsafepointer-t-throws-result"><div class="p">
    <p>Invokes the given closure with a pointer to the given argument.</p>

<p>The <code>withUnsafePointer(to:_:)</code> function is useful for calling Objective-C
APIs that take in parameters by const pointer.</p>

<p>The pointer argument to <code>body</code> is valid only during the execution of
<code>withUnsafePointer(to:_:)</code>. Do not store or return the pointer for later
use.</p>

<p><strong>Parameters:</strong>
  <strong>value:</strong> An instance to temporarily use via pointer. Note that the <code>inout</code>
    exclusivity rules mean that, like any other <code>inout</code> argument, <code>value</code>
    cannot be directly accessed by other code for the duration of <code>body</code>.
    Access must only occur through the pointer argument to <code>body</code> until
    <code>body</code> returns.
  <strong>body:</strong> A closure that takes a pointer to <code>value</code> as its sole argument. If
    the closure has a return value, that value is also used as the return
    value of the <code>withUnsafePointer(to:_:)</code> function. The pointer argument
    is valid only for the duration of the function&#39;s execution.
    It is undefined behavior to try to mutate through the pointer argument
    by converting it to <code>UnsafeMutablePointer</code> or any other mutable pointer
    type. If you need to mutate the argument through the pointer, use
    <code>withUnsafeMutablePointer(to:_:)</code> instead.
<strong>Returns:</strong> The return value, if any, of the <code>body</code> closure.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func withUnsafePointer&lt;T, Result&gt;(to value: inout T, _ body: (UnsafePointer&lt;T&gt;) throws -&gt; Result) rethrows -&gt; Result</code>
    
    
</div></div>
</div>
